package IO流.字符;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class 编码和解码 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //1.编码-->getBytes()
        String str="你好啊";
        byte[] bytes = str.getBytes();//默认使用UTF-8
        System.out.println(Arrays.toString(bytes));//[-28, -67, -96, -27, -91, -67, -27, -107, -118]

        //指定编码方式
        byte[] bytes1 = str.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));//[-60, -29, -70, -61, -80, -95]

        //2.解码---String的构造方法这里也是默认的UTF-8编码
        String str2=new String(bytes);
        System.out.println(str2);//你好啊

        //指定解码
        String gbk = new String(bytes1, "GBK");
        System.out.println(gbk);
    }
}
